package com.egdigital.allzic.ui.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.egdigital.allzic.R;
import com.egdigital.allzic.data.object.Station;
import com.egdigital.allzic.utils.ImageManager;
import com.egdigital.allzic.utils.Utils;

public class AdapterGridAllzic extends BaseAdapter {

	// log
	private static final String	TAG			= AdapterGridAllzic.class.getSimpleName();
	// data
	private List<Station>		mList;
	private LayoutInflater		mInflater	= null;
	private ImageManager		mImageManager;

	private class CallHolderAllZik {

		private ImageView	mImage;
	}

	public AdapterGridAllzic(List<Station> list, Context context) {
		mList = list;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// image manager
		mImageManager = ImageManager.getInstance(context);
		mImageManager.setOptions();
		mImageManager.setImageLoader();
	}

	@Override
	public int getCount() {
		return Integer.MAX_VALUE;
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Station station = mList.get(position % mList.size());
		// get
		CallHolderAllZik cellHolder = null;
		if (convertView == null) {
			cellHolder = new CallHolderAllZik();
			convertView = mInflater.inflate(R.layout.cell_grid_allzic, parent, false);
			cellHolder.mImage = (ImageView) convertView.findViewById(R.id.cell_allzic_image);
		} else {
			cellHolder = (CallHolderAllZik) convertView.getTag();
		}
		// set
		try {
			Utils.setImageFromUrl(mImageManager, station.getmThumbMobile(), cellHolder.mImage);
		} catch (Exception e) {
			Utils.setImageFromUrl(mImageManager, station.getmThumb(), cellHolder.mImage);
		}
		// end
		convertView.setTag(cellHolder);
		return convertView;
	}

	/**
	 * 
	 */
	public int getRealPosition(int position) {
		return position % mList.size();
	}
}
